package com.jiangjiang.eshop.inventory.updater;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.inventory.constant.GoodsStockStatus;
import com.jiangjiang.eshop.inventory.domain.GoodsStockEntity;
import com.jiangjiang.eshop.inventory.service.InventoryGoodsStockService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author Jianghaowen
 * @Date 2021/6/7
 * @Description: 库存更新命令工厂父类
 * @Version V1.0
 **/
@Slf4j
public abstract class AbstractStockUpdaterFactory<T> implements StockUpdaterFactory<T> {
    /**
     * 库存管理模块Service组件
     */
    protected InventoryGoodsStockService goodsStockService;

    /**
     * 日期辅助组件
     */
    protected DateHelper dateHelper;

    /**
     * 构造方法
     *
     * @param goodsStockService
     * @param dateHelper
     */
    public AbstractStockUpdaterFactory(
            InventoryGoodsStockService goodsStockService,
            DateHelper dateHelper) {
        this.goodsStockService = goodsStockService;
        this.dateHelper = dateHelper;
    }

    /**
     * 创建库存更新命令
     *
     * @param parameter 参数
     * @return
     */
    @Override
    public StockUpdater create(T parameter) {
        try {
            List<Long> goodsSkuIds = this.getGoodsSkuIds(parameter);
            List<GoodsStockEntity> goodsStock = this.getGoodsStock(goodsSkuIds);
            return createUpdater(goodsStock,parameter);
        } catch (Exception e){
            log.warn("AbstractStockUpdaterFactory create error",e);
        }
        return null;
    }
    /**
     * 获取商品sku 集合
     *
     * @param parameter 参数
     * @return 商品sku id 集合
     * @throws Exception
     */
    protected abstract List<Long> getGoodsSkuIds(T parameter) throws Exception;

    /**
     * 创建库存更新命令
     * @param goodsStock 库存数据
     * @param parameter 入参
     * @return 库存更新命令
     */
    protected abstract StockUpdater createUpdater(List<GoodsStockEntity> goodsStock, T parameter);

    /**
     * 根据sku id集合 获取商品库存记录如果没有库存记录，则新增一条
     *
     * @param goodsSkuIds
     * @return
     */
    private List<GoodsStockEntity> getGoodsStock(List<Long> goodsSkuIds) throws Exception {
        Map<Long, GoodsStockEntity> goodsStockMap = goodsStockService.queryByGoodsSkuIds(goodsSkuIds)
                .stream()
                .collect(Collectors.toMap(GoodsStockEntity::getGoodsSkuId, Function.identity()));

        List<GoodsStockEntity> needCreateStockList = Lists.newArrayList();
        for (Long goodsSkuId : goodsSkuIds) {
            if (Objects.isNull(goodsStockMap.get(goodsSkuId))) {
                // 创建一条库存记录
                GoodsStockEntity entity = GoodsStockEntity.builder()
                        .goodsSkuId(goodsSkuId)
                        .saledStockQuantity(0L)
                        .saleStockQuantity(0L)
                        .lockedStockQuantity(0L)
                        .stockStatus(GoodsStockStatus.NOT_IN_STOCK)
                        .gmtCreate(dateHelper.getCurrentDate())
                        .gmtModified(dateHelper.getCurrentDate())
                        .build();
                needCreateStockList.add(entity);
            }
        }
        goodsStockService.saveBatch(needCreateStockList);

        return Lists.newArrayList(Iterables.concat(needCreateStockList,goodsStockMap.values()));
    }



}
